package com.bnyy.medicalHousekeeper.moudle.message;

import android.content.Context;

import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.ContactUser;
import com.bnyy.message.bean.GroupChatSetting;
import com.bnyy.message.bean.chat.Chat;
import com.bnyy.message.bean.chat.ChatInfo;
import com.bnyy.message.enums.ChatType;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/9
 **/
public class ChatManager {

    public static void getChat(Chat chat, Consumer<Chat> consumer) {
        getChat(null, chat, consumer);
    }

    public static void getChat(Context context, Chat chat, Consumer<Chat> consumer) {
        if (chat != null) {
            if (chat.getContactUsers() == null) {
                HashMap<String, Object> params = new HashMap<>();
                RequestManager requestManager = RequestManager.getInstance();
                ChatType chatType = chat.getChatType();
                ArrayList<String> headers = new ArrayList<>();
                ArrayList<ContactUser> contactUsers = new ArrayList<>();

                if (chatType != null) {
                    params.put("id", chat.getRecipientId());
                    switch (chatType) {
                        case STRANGER:
                        case PRIVATE_CHAT:
                            params.put("role_id", chat.getRecipientRoleId());
                            requestManager.request(requestManager.mMessageRetrofitService.getPrivateChatSetting(RequestManager.getJsonRequestBody(params)),
                                    new BaseObserverImpl<ContactUser>(context) {
                                        @Override
                                        public void onSuccess(ContactUser contactUser) {
                                            super.onSuccess(contactUser);

                                            contactUsers.add(contactUser);
                                            chat.setContactUsers(contactUsers);

                                            ChatInfo chatInfo = new ChatInfo(chat.getChatId());
                                            headers.add(contactUser.getUser_img());
                                            chatInfo.setHeadersJson(new Gson().toJson(headers));
                                            chatInfo.setChatName(contactUser.getUsername());
                                            chatInfo.setTopping(contactUser.isTopping());
                                            chatInfo.setNoDisturb(contactUser.isNo_disturb());
                                            MessageManager.save(chatInfo);

                                            chat.setChatInfo(chatInfo);
                                            consumer.accept(chat);
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            super.onError(e);
                                            consumer.accept(chat);
                                        }

                                        @Override
                                        public void onOtherCode(int code) {
                                            super.onOtherCode(code);
                                            chat.setContactUsers(contactUsers);
                                            consumer.accept(chat);
                                        }
                                    });
                            break;
                        case GROUP_CHAT:
                            requestManager.request(requestManager.mMessageRetrofitService.getGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                                    new BaseObserverImpl<GroupChatSetting>(context) {
                                        @Override
                                        public void onSuccess(GroupChatSetting groupChatSetting) {
                                            super.onSuccess(groupChatSetting);

                                            for (ContactGroup.Member member : groupChatSetting.getUser_list()) {
                                                contactUsers.add(member);
                                                headers.add(member.getUser_img());
                                            }

                                            if (contactUsers.size() > 0) {
                                                chat.setContactUsers(contactUsers);
                                                ChatInfo chatInfo = new ChatInfo(chat.getChatId());
                                                chatInfo.setChatName(groupChatSetting.getGroup_name());
                                                chatInfo.setHeadersJson(new Gson().toJson(headers));
                                                chatInfo.setTopping(groupChatSetting.isTopping());
                                                chatInfo.setNoDisturb(groupChatSetting.isNoDisturb());
                                                MessageManager.save(chatInfo);

                                                chat.setChatInfo(chatInfo);
                                            }
                                            consumer.accept(chat);
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            super.onError(e);
                                            consumer.accept(chat);
                                        }

                                        @Override
                                        public void onOtherCode(int code) {
                                            super.onOtherCode(code);
                                            chat.setContactUsers(contactUsers);
                                            consumer.accept(chat);
                                        }

                                        @Override
                                        public void showMessage(String msg) {

                                        }
                                    });
                            break;
                    }
                }
            } else {
                consumer.accept(chat);
            }
        }
    }
}
